import datetime
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager


# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# time.sleep(2)
# driver.get("https://www.baidu.com")
# #3.查找文本框，输入搜索内容
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# #4.查找按钮，点击 百度一下
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# #5.关闭浏览器
# time.sleep(2)
# driver.quit()




#1.打开浏览器
#初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# time.sleep(2)
# driver.get("https://www.baidu.com")
# #搜索热搜第一条并点击
# time.sleep(2)
# driver.find_element(By.XPATH, "//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a").click()
# #5.关闭浏览器
# time.sleep(2)
# driver.quit()

# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# time.sleep(2)
# driver.get("https://www.baidu.com")
# #3.查找文本框，输入搜索内容
# time.sleep(2)
# ele = driver.find_element(By.CSS_SELECTOR, "#kw")
# ele.send_keys("迪丽热巴")
# ele.clear()
# ele.send_keys("古力娜扎")
# #4.查找按钮，点击 百度一下
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# #5.关闭浏览器
# time.sleep(2)
# driver.quit()

# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# time.sleep(2)
# driver.get("https://www.baidu.com")
# # #获取热搜文本
# # text = driver.find_element(By.CSS_SELECTOR, "#hotsearch-content-wrapper > li:nth-child(2) > a > span.title-content-title").text
# #获取 百度一下 文本
# text = driver.find_element(By.CSS_SELECTOR, "#su").get_attribute("value")
# print(text)
# assert text=="百度一下"
# #5.关闭浏览器
# time.sleep(2)
# driver.quit()

# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# time.sleep(2)
# driver.get("https://www.baidu.com")
# #通过标签页获取 标题 和 URL
# title = driver.title
# url = driver.current_url
# print(title)
# print(url)
# #5.关闭浏览器
# time.sleep(2)
# driver.quit()

# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# driver.get("https://www.baidu.com")
# # #找到 图片标签 并打开
# driver.find_element(By.CSS_SELECTOR, "#s-top-left > a:nth-child(6)").click()
# #获取当前页面句柄 和 所有页面句柄
# curWin = driver.current_window_handle
# allWin = driver.window_handles
# #遍历所有页面句柄，找到新打开的页面并切换
# for win in allWin:
#     if win != curWin:
#         driver.switch_to.window(win)
# # 查找 百度图片 页面的特有元素
# text = driver.find_element(By.XPATH, "//*[@id=\"app\"]/div/div[1]/div/div[4]/div[2]/div[1]/div/div/p/span[1]/span").text
# print(text)
# #关闭 百度图片 页面，切回 百度首页
# driver.close()
# driver.switch_to.window(curWin)
# #查找 百度一下 按钮
# text = driver.find_element(By.CSS_SELECTOR, "#su").get_attribute("value")
# print(text)
# #5.关闭浏览器
# driver.quit()


# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# time.sleep(2)
# driver.get("https://www.baidu.com")
# #窗口最大化
# driver.maximize_window()
# time.sleep(2)
# #窗口最小化
# driver.minimize_window()
# time.sleep(2)
# #窗口全屏
# driver.fullscreen_window()
# time.sleep(2)
# #自定义窗口大小
# driver.set_window_size(1024, 768)
# time.sleep(2)
# #5.关闭浏览器
# time.sleep(2)
# driver.quit()

# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# #2.进入百度首页
# driver.get("https://www.baidu.com")
# #屏幕截图
# filename = "autotest-" + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S") + ".png"
# driver.save_screenshot("./images/" + filename)
# #5.关闭浏览器
# driver.quit()

# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# # #2.警告弹窗页
# # driver.get("file:///C:/Users/13581/Desktop/Code/td/alter_html/alter.html")
# #2.提示弹窗页
# driver.get("file:///C:/Users/13581/Desktop/Code/td/alter_html/prompt.html")
# #找到按钮并点击，唤醒弹窗
# driver.find_element(By.CSS_SELECTOR, "body > button").click()
# #获取弹窗
# alter = driver.switch_to.alert
# #输入文本
# alter.send_keys("张三")
# time.sleep(2)
# #确认或者取消
# alter.accept()
# # alter.dismiss()
# time.sleep(2)
# #5.关闭浏览器
# driver.quit()

# #1.打开浏览器
# #初始化驱动
# ChromeInstall = ChromeDriverManager().install()
# #打开一个浏览器标签页
# driver = webdriver.Chrome(service=Service(ChromeInstall))
# # # 设置全局的隐式等待, 查找元素时休眠2秒
# # driver.implicitly_wait(2)


# # 打开百度
# driver.get("https://www.baidu.com")
# #设置 driver 等待时间
# wait = WebDriverWait(driver, 2)
# #设置等待条件
# wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#kw")))
# # 查找搜索框，输入 迪丽热巴
# driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("迪丽热巴")
# # 查找 百度一下 按钮并点击
# wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#su")))
# driver.find_element(By.CSS_SELECTOR, "#su").click()
# #5.关闭浏览器
# driver.quit()

# # 打开好看视频
# driver.get("https://haokan.baidu.com/?sfrom=baidu-top")
# # 点击热门视频
# driver.find_element(By.XPATH, "//*[@id=\"__next\"]/div/div[1]/div[1]/div[2]/div/div/div[1]/div[2]").click()
# # 回退到首页
# driver.back()
# time.sleep(2)
# #前进到热门视频
# driver.forward()
# time.sleep(2)
# #刷新页面
# driver.refresh()
# time.sleep(2)
# #5.关闭浏览器
# driver.quit()

# driver.get("file:///C:/Users/13581/Desktop/Code/td/alter_html/upload.html")
# #找到 选择文件 按钮
# ele = driver.find_element(By.XPATH, "//*[@id=\"fileUpload\"]")
# time.sleep(2)
# #上传文件
# ele.send_keys("C:\\Users\\13581\\Desktop\\Study\\test.pdf")
# time.sleep(5)
# #5.关闭浏览器
# driver.quit()

ChromeInstall = ChromeDriverManager().install()
options = webdriver.ChromeOptions()
#设置无头模式
# options.add_argument("-headless")
#页面加载策略
options.page_load_strategy = "none"
#初始化浏览器
driver = webdriver.Chrome(service=Service(ChromeInstall), options=options)

driver.get("https://www.baidu.com")
driver.find_element(By.CSS_SELECTOR, "#kw").send_keys("C++")
driver.find_element(By.CSS_SELECTOR, "#su").click()
driver.quit()